

import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;

import javax.swing.event.PopupMenuEvent;

import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.SWT;




/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class RemoteGUI extends org.eclipse.swt.widgets.Composite{
	private Button startButton;
	private Button contButton;
	private Button stopButton;
	private Button pauseButton;
	private static boolean start = false, pause = true;
	//private static boolean showRemote=true;
	private Listener listener;
	private static HoldService holder = HoldService.hold;
	public static Display display;//=Display.getDefault();
	public static Shell shell;// = new Shell(display);
	public static RemoteGUI inst;// = RemoteGUI.getRemote();//new RemoteGUI(shell, SWT.NULL);
	public static FillLayout thisLayout;
	private static Process process;
	//private static ConnectToServer connectSer = new ConnectToServer();
	private static Thread t;
	private static boolean running = true;
	//public static RemoteGUI remotegui;//= new RemoteGUI();
	/**
	* Overriding checkSubclass allows this class to extend org.eclipse.swt.widgets.Composite
	*/	
	protected void checkSubclass() {
	}
	
	public void openShell(){
		this.setVisible(true);
	}
	
	
/*	public void closeService(){
		showRemote = false;
		synchronized (display) {
			holder.stop();
			display.wake();
		}
	}*/
	
	
	public static void openMenu(){
		if(!SystemTray.isSupported())
        {
            System.out.println("System Tray is not supported!");
            return;
        }
		PopupMenu popMenu= new PopupMenu();
		MenuItem exit = new MenuItem("Exit");
		exit.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
            	running = false;
            	synchronized (inst) {
            		inst.notify();
				}
                System.exit(0);
            }
        });
		MenuItem remote = new MenuItem("Remote");
		remote.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
            	synchronized (inst) {
					inst.notify();
				}
            }
        });
		popMenu.add(remote);
		popMenu.addSeparator();
		popMenu.add(exit);
		
		Image img = Toolkit.getDefaultToolkit().getImage("C:/Users/HP/smile.jpg");
		TrayIcon trayIcon = new TrayIcon(img, "Application Name", popMenu);
		try {
			SystemTray.getSystemTray().add(trayIcon);
		} catch (AWTException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		while(running){
			showGUI();
			synchronized (inst) {
				try {
					inst.wait();
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		}
	}
	
	/**
	* Auto-generated method to display this 
	* org.eclipse.swt.widgets.Composite inside a new Shell.
	*/
	public static void showGUI() {
		display = Display.getDefault();
		shell = new Shell(display);
		inst = new RemoteGUI(shell, SWT.NULL);
		Point size = inst.getSize();
		shell.setLayout(new FillLayout());
		shell.layout();
		if(size.x == 0 && size.y == 0) {
			inst.pack();
			shell.pack();
		} else {
			Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
			shell.setSize(shellBounds.width, shellBounds.height);
		}
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		System.out.println("remote gui is closing");
	}
	
	public RemoteGUI(org.eclipse.swt.widgets.Composite parent, int style) {
		super(parent, style);
		initGUI();
		
	}

	private void initGUI() {
		try {
			thisLayout = new FillLayout(org.eclipse.swt.SWT.HORIZONTAL);
			this.setLayout(thisLayout);
			this.setSize(425, 86);
			{
				startButton = new Button(this, SWT.PUSH | SWT.CENTER);
				startButton.setText("Start");
				listener = new Listener() {
					public void handleEvent(Event event) {
			    		if(event.widget == startButton){
			    			synchronized (this) {
				    			try {
				    				//connect to server-and suspend
				    				//t=new Thread(connectSer);
				    				//t.start();
				    				
									startService();
				    				pause = false;
									start = true;
									update();
								} catch (Exception e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
			    			}
							
			    		}
			    	}
			    };      
			    startButton.addListener(SWT.Selection, listener);
			    //can press on button only if watch thread is not alive yet
			    startButton.setEnabled(!start);
			}
			{
				pauseButton = new Button(this, SWT.PUSH | SWT.CENTER);
				pauseButton.setText("Pause");
				listener = new Listener() {
			    	public void handleEvent(Event event) {
			    		if(event.widget == pauseButton){	
			    			synchronized (this) {
			    				try {
			    					holder.pause();
			    					//process.getOutputStream().wait();
			    					//String[] args={"pause"};
			    					//HoldService.main(args);
			    					//HoldService.class.
			    					//holder.pause();
									pause = true;
				    				update();
								} catch (Exception e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
			    			}
			    		}
			    	}
			    };      
			    pauseButton.addListener(SWT.Selection, listener);
			    //can press on button only if watch alive and pause is not working yet
			    pauseButton.setEnabled(start && !(pause));
			}
			{
				contButton = new Button(this, SWT.PUSH | SWT.CENTER);
				contButton.setText("Continue");
				listener = new Listener() {
			    	public void handleEvent(Event event) {
			    		if(event.widget == contButton){	
			    			//System.out.println("Resume");
			    			synchronized (this) {
			    				try {
			    					//process.getClass().cast(holder).notify();
			    					//process.getClass().newInstance().notify();
			    					holder.pause();
			    					//String[] args={"pause"};
			    					//HoldService.main(args);
			    					pause = false;
				    				update();
								} catch (Exception e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
			    			}
			    		}
			    	}

			    };      
			    contButton.addListener(SWT.Selection, listener);
			    //can press on button only if watch alive and pause is pressed
			    contButton.setEnabled(start && pause);
			}
			{
				stopButton = new Button(this, SWT.PUSH | SWT.CENTER);
				stopButton.setText("Stop");
				listener = new Listener() {
					@SuppressWarnings("deprecation")
					public void handleEvent(Event event) {
			    		if(event.widget == stopButton){	
			    			synchronized (this) {	
			    				try {
									stopService();
			    					//connectSer.updateServer("");
			    					//t.stop();
			    					System.out.println("stop connection");
			    					start = false;
				    				update();
								} catch (Exception e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
			    			}
			    		}
			    	}
			    };      
			    stopButton.addListener(SWT.Selection, listener);
			    //can press on button only if watch alive and pause is push
			    stopButton.setEnabled(start && !(pause));
			}   
			
			this.layout();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	static final String CMD_START = "cmd /c net start \"";
	static final String CMD_STOP = "cmd /c net stop \"";
	
	private static int startService() throws Exception {
		return execCmd(CMD_START+ "watchDirService\"");
	}

	private static int stopService() throws Exception {
		return execCmd(CMD_STOP + "watchDirService\"");
	}

	static int execCmd(String cmdLine) throws Exception {
		process = Runtime.getRuntime().exec(cmdLine);
		StreamPumper outPumper = new StreamPumper(process.getInputStream(), System.out);
		StreamPumper errPumper = new StreamPumper(process.getErrorStream(), System.err);
		outPumper.start();
		errPumper.start();
		process.waitFor();
		outPumper.join();
		errPumper.join();
		
		return process.exitValue();
	}


	
	
	static class StreamPumper extends Thread { 
		private InputStream is;
		private PrintStream os;

		public StreamPumper(InputStream is, PrintStream os) {
			this.is = is;
			this.os = os;
		}

		public void run() {
			try {
				BufferedReader br = new BufferedReader(new InputStreamReader(is));
				String line;

				while ((line = br.readLine()) != null)
					os.println(line);
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	
	
	@Override
	public void update(){
		startButton.setEnabled(!start);
		pauseButton.setEnabled(start && !(pause));
		contButton.setEnabled(start && pause);
		stopButton.setEnabled(start && !(pause));
	}

}
